package com.dingding.special.order.grab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.push.base.ClientAppType;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.code.order.OrderStatus;
import com.dingding.special.order.grab.dao.IUserGrabDao;
import com.dingding.special.order.grab.dto.UserGrabPushDTO;
import com.dingding.special.order.grab.sql.UserGrabSQL;

/**
 * 
 * @ClassName: UserGrabDaoImpl 
 * @Description: 用户抢单DAO实现类
 * @author YHQ
 * @date 2016年8月26日 上午10:42:13
 * @since V2.4.0
 * 
 */
@Component("userGrabDao")
public class UserGrabDaoImpl implements IUserGrabDao{

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name="criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	@Resource(name="SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	/**
	 * 
	 * @Title: geUserspecialorderT 
	 * @Description: 根据订单Id获取订单信息
	 * @param oid 订单Id
	 * @author YHQ 
	 * @since V2.4.0 
	 * @return UserspecialorderT    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public UserspecialorderT geUserspecialorderT(String oid) {
		
		try {
			return criteriaBaseDao.getById(oid, UserspecialorderT.class);
		} catch (Exception e) {
			log.error("根据订单Id获取订单信息时出现异常   类：" + this.getClass(), e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getUserGrabPushInfo 
	 * @Description: 获取用户抢单推送信息
	 * @param oid 订单Id
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return UserGrabPushDTO    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public UserGrabPushDTO getUserGrabPushInfo(String oid, String driverId) {
		
		try {
			
			// 获取用户抢单推送信息
			List<UserGrabPushDTO> userGrabPushDTOs = sqlBaseDao.listSqlToCustomBean(UserGrabPushDTO.class,UserGrabSQL.USER_GRAB_PUSH , driverId,oid);
			
			// 判断是否有推送信息
			if(userGrabPushDTOs != null && userGrabPushDTOs.size() > 0){
				
				UserGrabPushDTO dto = userGrabPushDTOs.get(0);
				if(dto.getLoginEquipment() == 1){
					dto.setClientType(ClientAppType.iOS.toString());
				}else{
					dto.setClientType(ClientAppType.Android.toString());
				}
				
				dto.setEstimateDistance(dto.getEstimateDistance());
				dto.setEstimateMoney(dto.getEstimateMoney());
				dto.setUserAvatar(ToolUtil.imageAddress(dto.getUserAvatar()));
				dto.setUserTabloidAvatar(ToolUtil.imageAddress(dto.getUserTabloidAvatar()));
				dto.setOrderStatus(OrderStatus.ORDER_STATUS_PAY);
				
				return userGrabPushDTOs.get(0);
			}
			
		} catch (Exception e) {
			log.error("", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getUserSpecialOrdertList 
	 * @Description: 根据用户Id获取其未完成订单列表
	 * @param userId 用户Id
	 * @author YHQ 
	 * @return List<Userspecialordert>    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public List<UserspecialorderT> getUserSpecialOrdertList(String userId) {
		
		Criterion criterion = Restrictions.eq("placeOrderUserId", userId);
		
		try {
			return criteriaBaseDao.queryCriteriaList(UserspecialorderT.class, criterion);
		} catch (Exception e) {
			log.error("根据用户Id获取其未完成订单列表时出现异常   类："+this.getClass()+" getUserSpecialOrdertList()", e);
		}
		
		return null;
	}

}
